package com.cloudkinto.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cloudkinto.dao.OrderBigcommerceDao;
import com.cloudkinto.entity.OrderBigcommerceShipaddressDo;
import com.cloudkinto.dao.OrderBigcommerceShipaddressDao;
import com.cloudkinto.service.order.OrderBigcommerceShipaddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.service.order.vo.bigcommerce.OrderBigComShippingAddressEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ding
 * @since 2022-11-08
 */
@Service
public class OrderBigcommerceShipaddressServiceImpl extends ServiceImpl<OrderBigcommerceShipaddressDao, OrderBigcommerceShipaddressDo> implements OrderBigcommerceShipaddressService {
    @Autowired
    private OrderBigcommerceShipaddressDao dao;

    @Override
    public void addEntityList(List<OrderBigComShippingAddressEntity> shippingAddressEntityList, Long id) {
        for (OrderBigComShippingAddressEntity orderBigComShippingAddressEntity : shippingAddressEntityList) {
            OrderBigcommerceShipaddressDo orderBigcommerceShipaddressDo = dao.selectOne(new LambdaQueryWrapper<OrderBigcommerceShipaddressDo>()
                    .eq(OrderBigcommerceShipaddressDo::getShippingId, orderBigComShippingAddressEntity.getId())
                    .eq(OrderBigcommerceShipaddressDo::getOrderId, id)
                    .last("limit 1"));
            if (orderBigcommerceShipaddressDo == null) {
               buildDo(orderBigComShippingAddressEntity,orderBigcommerceShipaddressDo);
                dao.insert(orderBigcommerceShipaddressDo);
            } else {
                buildDo(orderBigComShippingAddressEntity,orderBigcommerceShipaddressDo);
                orderBigcommerceShipaddressDo.setShippingId(orderBigComShippingAddressEntity.getId());
                dao.updateById(orderBigcommerceShipaddressDo);
            }
        }
    }

    private void buildDo(OrderBigComShippingAddressEntity orderBigComShippingAddressEntity, OrderBigcommerceShipaddressDo orderBigcommerceShipaddressDo) {
        orderBigcommerceShipaddressDo.setShippingName(orderBigComShippingAddressEntity.getLastName() + " " + orderBigComShippingAddressEntity.getFirstName());
        orderBigcommerceShipaddressDo.setShippingCompany(orderBigComShippingAddressEntity.getCompany());
        orderBigcommerceShipaddressDo.setShippingCity(orderBigComShippingAddressEntity.getCity());
        orderBigcommerceShipaddressDo.setShippingAddress2(orderBigComShippingAddressEntity.getStreet2());
        orderBigcommerceShipaddressDo.setShippingAddress1(orderBigComShippingAddressEntity.getStreet1());
        orderBigcommerceShipaddressDo.setShippingCountry(orderBigComShippingAddressEntity.getCountry());
        orderBigcommerceShipaddressDo.setShippingState(orderBigComShippingAddressEntity.getState());
        orderBigcommerceShipaddressDo.setShippingPhone(orderBigComShippingAddressEntity.getPhone());
        orderBigcommerceShipaddressDo.setShippingZip(orderBigComShippingAddressEntity.getZip());
    }
}
